﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using UnityEngine.EventSystems;
// using System;

/*
生成敌机，卫星
 */
public class Spawn : MonoBehaviour {

	public Star star;//当前星球对象
	public GameObject guardPrefab;//卫星预设体
	public GameObject smallEnemyPlanePrefab;//小型敌机预设体
	public GameObject middleEnemyPlanePrefab;//中型敌机预设体
	public GameObject bigEnemyPlanePrefab;//大型敌机预设体
	public GameObject gameObjTemp;//当前创建的对象

	public float searchRange;//搜索范围
	public bool isAttack = false;
	public CircleCollider2D collider;
	public GameObject target;

	private int smallNum = 5;//小型敌机数量
	private int middleNum = 1;//中型敌机数量
	private int bigNum = 0;//大型敌机数量
	private int otherNum = 0;//其他敌机
	private int storeSmallNum = 0;//机库小型敌机数
	private int storeMiddleNum = 0;//机库中型敌机数
	private int storeBigNum = 0;//机库大型敌机数
	private bool boss = false;//是否有boss产生
	private float makeSmallTime = 10;//小型敌机生产时间
	private float makeMiddleTime = 20;//中型敌机生产时间
	private float makeBigTime = 30;//大型敌机生产时间
	private float currSmallTime;//当前小型敌机生产时间
	private float currMiddleTime;//当前中型敌机生产时间
	private float currBigTime;//当前大型敌机生产时间
	private float makeOtherTime = 25;//其他敌机产生时间



	private int guardNum ;//卫星数量
	private int minNum = 1;//最少卫星数
	private int maxNum = 3;//最多卫星数
	private float guardRotateR;//卫星旋转旋转半径
	private float minR = 8;//卫星最小环绕半径
	private float maxR = 11;//卫星最大环绕半径
	Vector3 guardRotateDir;//卫星旋转方向
	private float guardRotateSpeed;//卫星环绕速度
	private float minSpeed = 40;//最小速度
	private float maxSpeed = 80;//最大速度


	private int starPower;//星球技术力（卫星强度，护盾血量，星球护甲， 星球血量，搜索范围，存储的敌机数量，敌机产生时间，是否有boss）
	private int producePower;//造船技术力（速度，血量，防御）
	private int planePower;//兵装技术力（产生的敌机强度（武器））

	public static System.Action<GameObject> SetTarget;


	// Use this for initialization
	void Start () {
		starPower = star.GetStarPower();
		producePower = star.GetProducePower();
		planePower = star.GetPlanePower();

		collider = this.GetComponent<CircleCollider2D>();

		//一开始星球所储存的飞机数量
		storeSmallNum = smallNum;
		storeMiddleNum = middleNum;
		storeBigNum = bigNum;


		InitGuards(starPower);
		InitSearchRange(starPower);

	}

	// Update is called once per frame
	void Update () {
		if(star.state != Star.state_death)
		{
			ProducePlane(starPower,producePower,planePower);
		}
	}

	//实例化卫星
	private void InitGuards(int starPower)
	{
		/*
		根据starPower设置卫星数量
		 */
		guardNum = Random.Range(minNum, maxNum);
		for (int i = 0; i < guardNum; i++)
		{
			//随机生成属性
			guardRotateR = Random.Range(minR, maxR);
			guardRotateDir = GetGuardRotateDir();
			guardRotateSpeed = Random.Range(minSpeed, maxSpeed);

			// 实例化卫星
			gameObjTemp = GameObject.Instantiate(guardPrefab, new Vector3(transform.position.x + guardRotateR, transform.position.y, 0), transform.rotation);
			gameObjTemp.transform.SetParent(transform.parent);
			gameObjTemp.GetComponent<Guard>().SetRotatePro(transform.parent.transform, GetGuardRotateDir(), guardRotateSpeed);
			SetEnemyPlanePro(gameObjTemp);
			/*
			根据starPower设置卫星武器
			 */
		}
	}

	//随机获得卫星旋转方向
	private Vector3 GetGuardRotateDir()
	{
		Vector3[] guardRotateDirs = new Vector3[]{Vector3.forward, -Vector3.forward};//旋转方向集合
		return guardRotateDirs[(int)Random.Range(0, 2)];
	}


	private void InitSearchRange(int starPower)
	{
		/*
		根据starPower设置搜索范围
		 */
		searchRange = 10f;
		collider.radius = searchRange;
	}




	// 生产飞机
	private void ProducePlane(int starPower, int producePower, int planePower)
	{
		// print("starPower--->" + starPower);
		// print("producePower--->" + producePower);
		// print("planePower--->" + planePower);
		if(star.state == Star.state_attack)
		{
			CreatStorePlane();	//实例化所有储存的飞机，储存量归零
			CreatePlane();	// 每隔一段时间实例化飞机
		}
		else
		{
			StorePlane();	// 每隔一段时间储存一架飞机
		}
	}


	//实例化所有储存的飞机 
	private void CreatStorePlane()
	{
		for (int i = 0; i < storeSmallNum; storeSmallNum--)
		{
//			print("小飞机");
			gameObjTemp = GameObject.Instantiate(smallEnemyPlanePrefab, GetCreatEnemyPlanePosition(transform.position), transform.rotation);
			SetEnemyPlanePro(gameObjTemp);
			/*
			设置小型飞机属性
			 */
		}
		WaitTime (0.5f);
		for (int i = 0; i < storeMiddleNum;  storeMiddleNum--)
		{
//			print("中飞机");
			gameObjTemp = GameObject.Instantiate(middleEnemyPlanePrefab, GetCreatEnemyPlanePosition(transform.position), transform.rotation);
			SetEnemyPlanePro(gameObjTemp);
			/*
			设置中型飞机属性
			 */
		}
		WaitTime (0.5f);
		for (int i = 0; i < storeBigNum;  storeBigNum--)
		{
//			print("大飞机");
			gameObjTemp = GameObject.Instantiate(bigEnemyPlanePrefab, GetCreatEnemyPlanePosition(transform.position), transform.rotation);
			SetEnemyPlanePro(gameObjTemp);
			/*
			设置大型飞机属性
			 */
		}
	}

	//实例化飞机
	private void CreatePlane()
	{
		currSmallTime += Time.deltaTime;
		currMiddleTime += Time.deltaTime;
		currBigTime += Time.deltaTime;
		if(currSmallTime >= makeSmallTime)
		{
			gameObjTemp = GameObject.Instantiate(smallEnemyPlanePrefab, GetCreatEnemyPlanePosition(transform.position), transform.rotation);
			SetEnemyPlanePro(gameObjTemp);
			currSmallTime = 0;
		}
		WaitTime (0.5f);
		if(currMiddleTime >= makeMiddleTime)
		{
			gameObjTemp = GameObject.Instantiate(middleEnemyPlanePrefab, GetCreatEnemyPlanePosition(transform.position), transform.rotation);
			SetEnemyPlanePro(gameObjTemp);
			currMiddleTime = 0;
		}
		WaitTime (0.5f);
		if(currBigTime >= makeBigTime)
		{
			gameObjTemp = GameObject.Instantiate(bigEnemyPlanePrefab, GetCreatEnemyPlanePosition(transform.position), transform.rotation);
			SetEnemyPlanePro(gameObjTemp);
			currBigTime = 0;
		}
	}

	// 储存飞机进仓库
	private void StorePlane()
	{
		currSmallTime += Time.deltaTime;
		currMiddleTime += Time.deltaTime;
		currBigTime += Time.deltaTime;
		if(currSmallTime >= makeSmallTime)
		{
			storeSmallNum++;
			currSmallTime = 0;
		}
		if(currMiddleTime >= makeMiddleTime)
		{
			storeMiddleNum++;
			currMiddleTime = 0;
		}
		if(currBigTime >= makeBigTime)
		{
			storeBigNum++;
			currBigTime = 0;
		}
	}

	private Vector3 GetCreatEnemyPlanePosition(Vector3 position)
	{
		float x = Random.Range(-6, 6);
		float y = Random.Range(-6, 6);
		return new Vector3((position.x + x), (position.y + y), 0);
	}


	// 设置不同的敌机属性
	private void SetEnemyPlanePro(GameObject gameObjTemp)
	{
		// 给Gun设置武器目标和武器
		gameObjTemp.transform.Find("gun").GetComponent<EnemyGun>().SetTarget(GameObject.Find("Hero"));
		gameObjTemp.transform.Find("gun").GetComponent<EnemyGun>().SetWeapon(GetWeaponInfoByPlanePowerAndEnemy(gameObjTemp.name));

		// 设置飞机属性
		switch (gameObjTemp.name)
		{
		case "smallEnemyPlane(Clone)":
			gameObjTemp.GetComponent<SmallEnemyPlane>().setProducePower(producePower);
			gameObjTemp.GetComponent<SmallEnemyPlane>().SetTarget(GameObject.Find("Hero"));
			gameObjTemp.GetComponent<SmallEnemyPlane>().setStar(star);
			break;
		case "middleEnemyPlane(Clone)":
			gameObjTemp.GetComponent<MiddleEnemyPlane>().setProducePower(producePower);
			gameObjTemp.GetComponent<MiddleEnemyPlane>().SetTarget(GameObject.Find("Hero"));
			gameObjTemp.GetComponent<MiddleEnemyPlane>().setStar(star);
			break;
		case "bigEnemyPlane(Clone)":
			gameObjTemp.GetComponent<BigEnemyPlane>().setProducePower(producePower);
			gameObjTemp.GetComponent<BigEnemyPlane>().SetTarget(GameObject.Find("Hero"));
			gameObjTemp.GetComponent<BigEnemyPlane>().setStar(star);
			break;
		default :
			break;
		}

	}


	//根据planePower和不同敌人获得不同武器 
	private WeaponInfo GetWeaponInfoByPlanePowerAndEnemy(string enemyName)
	{
		/*
		根据PlanePower获得敌人的武器
		 */

		switch (enemyName)
		{
		case "smallEnemyPlane(Clone)":
			if(planePower == 5)
			{
				return DataLists.instance.FindWeaponInfoById(2);
			}
			else
			{
				return null;
			}
		case "middleEnemyPlane(Clone)":
			if(planePower == 5)
			{
				return DataLists.instance.FindWeaponInfoById(1);
			}
			else
			{
				return null;
			}
		case "bigEnemyPlane(Clone)":
			if(planePower == 5)
			{
				return DataLists.instance.FindWeaponInfoById(1);
			}
			else
			{
				return null;
			}
		case "guard(Clone)":
			if(starPower == 5)
			{
				return DataLists.instance.FindWeaponInfoById(0);
			}
			else
			{
				return null;
			}
		default :
			return null;
		}
	}






	void OnTriggerEnter2D(Collider2D collision)
	{
		if(collision.tag == "Hero")
		{
			isAttack = true;
			if (star.state != Star.state_death) 
			{
				star.SetState(Star.state_attack);
			}
			// target = 
			if(SetTarget != null)
			{
				SetTarget(collision.gameObject);
			}
		}
	}


	void OnTriggerExit2D(Collider2D collision)
	{
		if(collision.tag == "Hero")
		{
			//			isAttack = false;
			//			star.SetState(Star.state_defend);

		}
	}
	void WaitTime(float time)
	{
		StartCoroutine (IEWaitTime(time));
	}

	IEnumerator IEWaitTime(float time)
	{
		yield return new WaitForSeconds (time);
	}
}
